<template>
	<view class="report-serve">
		<UserDate @changeUser='onChangeUser' @changeDate='onChangeDate'></UserDate>
		<u-tabs :list="list" :is-scroll="false" :current="current" :active-color='themeColorLight' @change="change"></u-tabs>
		<view class="report-serve-main">
			<view v-for="(item,index) in listData" :key="index" class="card" style="margin-top: 10rpx;">
				<view class="div-cut"></view>
				<view class="title u-border-bottom">
					<view class="left">
						<text class="iconfont icon-baogaoguanli_huaban1"></text>
						<text v-if="current===2">体检编号：{{item.tjbh||''}}</text>
						<text v-else>报告单号：{{item.repno||''}}</text>
					</view>
					<view class="right">
					</view>
				</view>
				<view class="main flexR">
					<view class="left" v-if="current===2">
						<view class="row"><text>姓&emsp;&emsp;名：</text>{{item.name||''}}</view>
						<view class="row"><text>体检分类：</text>{{item.healthTypeName}}</view>
						<view class="row"><text>体检时间：</text>{{item.inspectionDate}}</view>
						<view class="row"><text>体检状态：</text>{{item.statusName}}</view>
					</view>
					<view class="left" v-else>
						<view class="row" v-for="cItem in cardLabel" :key='cItem.fKey'> <text>{{cItem.label}}</text>{{item[cItem.key]||''}} </view>
						<view class="row" v-if="item.bgxzlj" @click="downLoad(item.bgxzlj)"><text>操&emsp;&emsp;作：</text><label style="color:#007AFF">下载报告</label></view>
					</view>
					<view class="right" @click="toPage(item)" v-if="current!=2||item.status==='3'">
						<u-icon name="arrow-right" color="#B5B5B5"></u-icon>
					</view>
				</view>
			</view>
			<u-empty text='暂无报告信息' mode="list" v-if="!load&&listData.length===0"></u-empty>
		</view>
		<Load :load="load" />
	</view>
</template>

<script>
	import UserDate from '@/components/UserInfo/user-date.vue'
	import Load from '@/components/Load/index.vue'
	import { themeColorLight } from '@/static/js/theme.js'
	import { mapGetters } from 'vuex'
	import { getCheckExamList, getPhysicalList } from '@/api/home.js'
	import { getHealthRecord } from '@/api/physical.js'
	import {defaultDate} from '@/common/js/default-date.js'
	export default {
		components: { UserDate, Load },
		data() {
			return {
				themeColorLight,
				load:false,
				list:[{name:'检查报告'},{name:'检验报告'},{name:'体检报告'}],
				current:0,
				jcjybz:'0', // 检查检验标志  0 检查 1 检验
				listData:[{}],
				cardLabel:[
					{
						label:'患者姓名：',
						key:'hzxm',
						fKey:'label11'
					},
					{
						label:'申请医生：',
						key:'sqys',
						fKey:'label21'
					},
					{
						label:'报告医生：',
						key:'bgys',
						fKey:'label31'
					},
					{
						label:'项目类别：',
						key:'replbmc',
						fKey:'label4'
					},
					{
						label:'检查时间：',
						key:'jcsj',
						fKey:'label5'
					},
					{
						label:'系统标志：',
						key:'xtbzName',
						fKey:'label32'
					}
				],
				query:{}
			}
		},
		computed:{
			...mapGetters(['userInfo'])
		},
		async onLoad() {
			await this.$onLaunched
			this.query = defaultDate
			this.initData()
		},
		methods: {
			change(val){
				this.current = val
				if(val<2){
					this.jcjybz = val
				}
				this.initData()
			},
			toPage(item){
				if(this.current===2){
					uni.navigateTo({
						url:'/physical/PhysicalServe/report?healthNo=' + item.healthNo
					})
				}else{
					uni.navigateTo({
						url:`/pages/Service/ReportServe/examination?tjbh=${item.tjbh}`
					})
				}
			},
			initData(){
				if(!this.userInfo.patId){
					uni.navigateTo({
						// url:'/pages/Login/index'
						url:'/pages/Login/addPatient'
					})
					return
				}
				this.load = true
				// 检查检验
				if(this.current===0||this.current===1){
					let params = {
						...this.query,
						patId : this.userInfo.patId,
						jcjybz : this.jcjybz
					}
					getCheckExamList(params).then(data => {
						this.load = false
						let res = data||[]
						res.forEach(item => {
							if(item.xtbz==='0'){
								item.xtbzName = '门诊'
							}else if(item.xtbz==='1'){
								item.xtbzName = '住院'
							}
						})
						this.listData = res
					}).catch(e => {
						this.load = false
						this.listData = []
					})
				}else{ // 体检
					let params = {
						...this.query,
						patientId : this.userInfo.patId
					}
					getHealthRecord(params).then(data => {
						this.load = false
						let res = data||[]
						res.forEach(item => {
							item.statusName = item.status==1?'未体检':item.status==2?'体检中':item.status==3?'已完成':''
						})
						this.listData = res
					}).catch(e => {
						this.load = false
						this.listData = []
					})
				}
			},
			onChangeUser(){
				this.initData()
			},
			onChangeDate(val){
				this.query = val
				this.initData()
			},
			downLoad(url){
				const _this = this
				uni.downloadFile({
					url,     //文件链接
					success:({statusCode, tempFIlePath})=>{
						//statusCode状态为200表示请求成功，tempFIlePath临时路径
						if(statusCode==200){
							//保存到本地
							uni.saveFile({
								tempFIlePath,
								success:(res)=>{
									//res.savedFilePath文件的保存路径
									_this.$u.toast('文件已保存在：' + res.savedFilePath)
									//保存成功并打开文件
									setTimeout(() => {
										uni.openDocument({
											filePath: res.savedFilePath,
											success: function(res) {
												// console.log('打开文档成功');
											}
										});
									}, 2000)
								},
								fail:()=>console.log('下载失败')
							})
						}
					},
					fail:()=>{ _this.$u.toast('文件下载失败，请重试') }
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.report-serve{
		height: calc(100vh);
		background-color: $uni-bg-color;
		&-main{
			height: calc(100vh - 190rpx);
			overflow: scroll;
		}
	}
</style>
